package com.example.day04_fragment;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class HttpUtils {
    private static Handler handler = new Handler(Looper.getMainLooper());//目的是可以在非主线程中刷新UI界面
    private static Gson gson = new Gson();

    public static <T> void doGet(String url, Map<String,?> query, Class<T> clazz, Consumer<T> success,Consumer<Throwable> fail){
        if(query!=null){
            url+="?"+query.entrySet().stream().map(e->e.getKey()+"="+e.getValue()).collect(Collectors.joining("&"));
        }
        String path = url;
        new Thread(()->{
            InputStream is = null;
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
                connection.setRequestMethod("GET");
                if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                    is = connection.getInputStream();
                    T t = gson.fromJson(new JsonReader(new InputStreamReader(is)),clazz);
                    handler.post(()->{success.accept(t);});
                }else{
                    throw  new RuntimeException(String.valueOf(connection.getResponseCode()));
                }
            } catch (IOException e) {
                handler.post(()->{fail.accept(e);});
            }finally {
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }
    public static <T> void doPost(String url,Map<String,?> map,Class<T> clazz,Consumer<T> success,Consumer<Throwable> fail){
        String path = url;
       new Thread(()->{
           InputStream is = null;
           OutputStream os = null;
           try {
               HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
               connection.setRequestMethod("POST");
               connection.setRequestProperty("content-type","application/json");
               os = connection.getOutputStream();
               os.write(gson.toJson(map).getBytes(StandardCharsets.UTF_8));
               os.flush();
               if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                   is = connection.getInputStream();
                   T t = gson.fromJson(new JsonReader(new InputStreamReader(is)), clazz);
                   handler.post(()->{success.accept(t);});
               }else{
                   throw new RuntimeException(String.valueOf(connection.getResponseCode()));
               }
           } catch (IOException e) {
               handler.post(()->{fail.accept(e);});
           }finally {
               if(is!=null){
                   try {
                       is.close();
                   } catch (IOException e) {
                       throw new RuntimeException(e);
                   }
               }
               if(os!=null){
                   try {
                       os.close();
                   } catch (IOException e) {
                       throw new RuntimeException(e);
                   }
               }
           }
       }).start();

    }
}
